<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\fetch;

use app\enum\ConfigEnum;
use app\enum\OceanEnum;
use app\service\fetch\ocean\AppExtendService;
use app\service\fetch\ocean\AssetsService;
use app\service\fetch\ocean\AudiencePackageService;
use app\service\fetch\ocean\CarouselAllService;
use app\service\fetch\ocean\CarouselService;
use app\service\fetch\ocean\ClueProductService;
use app\service\fetch\ocean\ImageAllService;
use app\service\fetch\ocean\ImageService;
use app\service\fetch\ocean\MongoEfficiencyVideoService;
use app\service\fetch\ocean\MongoPicService;
use app\service\fetch\ocean\MongoVideoAllService;
use app\service\fetch\ocean\MongoVideoService;
use app\service\fetch\ocean\MongoVideoTagAllService;
use app\service\fetch\ocean\MongoVideoTagService;
use app\service\fetch\ocean\NativeAnchorService;
use app\service\fetch\ocean\OptimizedGoalService;
use app\service\fetch\ocean\ProductPlatformService;
use app\service\fetch\ocean\ProductService;
use app\service\fetch\ocean\ProjectService;
use app\service\fetch\ocean\PromotionService;
use app\service\fetch\ocean\RebateMaterialService;
use app\service\fetch\ocean\SchemaPicService;
use app\service\fetch\ocean\SchemaVideoEfficiencyService;
use app\service\fetch\ocean\SchemaVideoService;
use app\service\fetch\ocean\SourcePicService;
use app\service\fetch\ocean\SourceVideoService;
use app\service\fetch\ocean\ToolsGrayService;
use app\service\fetch\ocean\TrackUrlService;
use app\service\fetch\ocean\VideoCoverService;
use app\service\fetch\ocean\VideoService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\facade\Cache;

class OceanFetchService extends FetchService
{
    /**
     * 拉取应用分包.
     * @param string $advertiserId 账户id
     */
    public static function fetchAppExtend(string $advertiserId = ''): bool
    {
        $key = AppExtendService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = AppExtendService::getAppExtend($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 拉取项目列表.
     * @param string $advertiserId 账户id
     */
    public static function fetchProjectList(string $advertiserId = ''): bool
    {
        $key = ProjectService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = ProjectService::getProjectList($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 拉取广告列表.
     * @param string $advertiserId 账户id
     */
    public static function fetchPromotionList(string $advertiserId = ''): bool
    {
        $key = PromotionService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = PromotionService::getPromotionList($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 拉取原生锚点列表.
     * @param string $advertiserId 账户id
     */
    public static function fetchNativeAnchorList(string $advertiserId = ''): bool
    {
        $key = NativeAnchorService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = NativeAnchorService::getNativeAnchorList($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 拉取商品库信息.
     * @param string $advertiserId 账户id
     */
    public static function fetchProductPlatform(string $advertiserId = ''): bool
    {
        $key = ProductPlatformService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = ProductPlatformService::getProductPlatform($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 拉取商品列表.
     * @param string $advertiserId 账户id
     */
    public static function fetchProductList(string $advertiserId = ''): bool
    {
        $key = ProductService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = ProductService::getProductList($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 拉取线索商品列表.
     * @param string $advId 账户ID
     */
    public static function fetchClueProductList(string $advId = ''): bool
    {
        $key = ClueProductService::getFetchKey($advId);
        if (self::lock($key, $advId)) {
            $res = ClueProductService::getClueProductList($advId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 拉取视频列表.
     * @param string $advertiserId 账户id
     */
    public static function fetchVideoList(string $advertiserId = ''): bool
    {
        $key = VideoService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = VideoService::getVideoList($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 拉取视频封面.
     * @param string $advertiserId 账户id
     */
    public static function fetchVideoCover(string $advertiserId = ''): bool
    {
        $key = VideoCoverService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = VideoCoverService::getVideoCover($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 拉取增量图片列表.
     * @param string $advertiserId 账户id
     */
    public static function fetchImageList(string $advertiserId = ''): bool
    {
        $key = ImageService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = ImageService::getImageList($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 拉取全量图片列表.
     * @param string $advertiserId 账户id
     */
    public static function fetchImageAllList(string $advertiserId = ''): bool
    {
        $key = ImageAllService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = ImageAllService::getImageList($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * mongo拉取视频增量列表.
     * @param string $advertiserId 账户id
     */
    public static function fetchMongoVideoList(string $advertiserId = ''): bool
    {
        $key = MongoVideoService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = MongoVideoService::getVideoList($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * mongo拉取低效视频列表.
     * @param string $advId 账户id
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function fetchMongoEfficiencyVideoList(string $advId = ''): bool
    {
        $key = MongoEfficiencyVideoService::getFetchKey($advId);
        if (self::lock($key, $advId)) {
            $res = MongoEfficiencyVideoService::getVideoList($advId);
            self::unlock($key);
            // 加入直达链接缓存队列
            Cache::store('redis')->handler()->rPush(ConfigEnum::CACHE_CONFIG[OceanEnum::LIST_CONFIG_KEY]['schema_video_efficiency'], $advId);
            return $res;
        }
        return true;
    }

    /**
     * mongo拉取视频全量列表.
     * @param string $advertiserId 账户id
     */
    public static function fetchMongoVideoAllList(string $advertiserId = ''): bool
    {
        $key = MongoVideoAllService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = MongoVideoAllService::getVideoList($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * mongo拉取图片列表.
     * @param string $advertiserId 账户id
     */
    public static function fetchMongoPicList(string $advertiserId = ''): bool
    {
        $key = MongoPicService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = MongoPicService::getPicList($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 拉取定向包.
     * @param string $advertiserId 账户id
     */
    public static function fetchAudiencePackage(string $advertiserId = ''): bool
    {
        $key = AudiencePackageService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = AudiencePackageService::getAudiencePackage($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 拉取直达链接视频.
     * @param string $idStr 队列信息
     */
    public static function fetchSchemaVideo(string $idStr = ''): bool
    {
        $key = SchemaVideoService::getFetchKey($idStr);
        if (self::lock($key, $idStr)) {
            $res = SchemaVideoService::getVideoList($idStr);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 拉取直达链接视频.
     * @param string $idStr 队列信息
     */
    public static function fetchSourceVideo(string $idStr = ''): bool
    {
        $key = SourceVideoService::getFetchKey($idStr);
        if (self::lock($key, $idStr)) {
            $res = SourceVideoService::getVideoList($idStr);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 拉取直达链接图片.
     * @param string $idStr 队列信息
     */
    public static function fetchSchemaPic(string $idStr = ''): bool
    {
        $key = SchemaPicService::getFetchKey($idStr);
        if (self::lock($key, $idStr)) {
            $res = SchemaPicService::getPicList($idStr);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 拉取直达链接图片.
     * @param string $idStr 队列信息
     */
    public static function fetchSourcePic(string $idStr = ''): bool
    {
        $key = SourcePicService::getFetchKey($idStr);
        if (self::lock($key, $idStr)) {
            $res = SourcePicService::getPicList($idStr);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 删除直达链接低效素材.
     * @param string $advertiserId 广告主id
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function fetchSchemaVideoEfficiency(string $advertiserId = ''): bool
    {
        $key = SchemaVideoEfficiencyService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = SchemaVideoEfficiencyService::getVideoList($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * mongo拉取视频标签增量列表.
     *
     * @param string $advertiserId 账户id
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function fetchMongoVideoTagList(string $advertiserId = ''): bool
    {
        $key = MongoVideoTagService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = MongoVideoTagService::getTagList($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * mongo拉取视频标签全量列表.
     *
     * @param string $advertiserId 账户id
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function fetchMongoVideoTagAllList(string $advertiserId = ''): bool
    {
        $key = MongoVideoTagAllService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = MongoVideoTagAllService::getTagList($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 获取低质素材.
     *
     * @param string $agentId 代理商id
     * @param string $month 月份
     */
    public static function fetchRebateMaterialList(string $agentId = '', string $month = ''): bool
    {
        return RebateMaterialService::getRebateMaterial($agentId, $month);
    }

    /**
     * 同步可用优化目标（巨量广告升级版）列表.
     * @param string $advertiserId 账户id
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function fetchOptimizedGoalList(string $advertiserId = ''): bool
    {
        return OptimizedGoalService::getOptimizedGoal($advertiserId);
    }

    /**
     * 拉取巨量推广内容数据.
     * @param string $advertiserId 账户id
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function fetchAssetsList(string $advertiserId = ''): bool
    {
        $key = AssetsService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = AssetsService::getAssets($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 拉取同步巨量事件资产下的监测链接组.
     * @param string $advertiserId 账户id
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function fetchTrackUrlList(string $advertiserId = ''): bool
    {
        return TrackUrlService::getTrackUrl($advertiserId);
    }

    /**
     * 拉取图文素材增量列表.
     *
     * @param string $advertiserId 账户id
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function fetchCarouselList(string $advertiserId = ''): bool
    {
        $key = CarouselService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = CarouselService::getCarouselList($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     *拉取图文素材全量列表.
     *
     * @param string $advertiserId 账户id
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function fetchCarouselAllList(string $advertiserId = ''): bool
    {
        $key = CarouselAllService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = CarouselAllService::getCarouselList($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }

    /**
     * 拉取巨量白名单能力状态.
     * @param string $advertiserId 账户
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public static function fetchToolsGray(string $advertiserId = ''): bool
    {
        $key = ToolsGrayService::getFetchKey($advertiserId);
        if (self::lock($key, $advertiserId)) {
            $res = ToolsGrayService::getToolsGray($advertiserId);
            self::unlock($key);
            return $res;
        }
        return true;
    }
}
